﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;

using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Spreadsheets;

namespace DataSources.Watcher
{
    public class GoogleSpreadsheets : DataSources.GoogleSpreadsheets, IDataWatcher
    {
        Hashtable lastUpdateTimes = new Hashtable();

        public GoogleSpreadsheets(string login, string password) :
            base(login, password)
        {
            ResetLastUpdated();
        }

        public void ResetLastUpdated()
        {
            lastUpdateTimes.Clear();
            foreach (DictionaryEntry spreadsheet in spreadsheets)
            {
                lastUpdateTimes.Add(spreadsheet.Key, ((SpreadsheetInfo)spreadsheet.Value).entry.Updated);
            }
        }

        public void RefreshLastUpdated(string entryHash, string newCacheValue)
        {
            // useless
        }

        public IDataWatcher GetChildDataSource(string entryHash)
        {
            if (this.spreadsheets.Contains(entryHash))
                return new GoogleWorksheets(this.service, this, entryHash);
            return null;
        }

        public Hashtable GetUpdatedEntries()
        {
            Hashtable updatedEntries = new Hashtable();
            foreach (DictionaryEntry spreadsheet in spreadsheets)
            {
                SpreadsheetInfo info = (SpreadsheetInfo)spreadsheet.Value;
                if (info.entry.Updated.CompareTo(lastUpdateTimes[spreadsheet.Key]) > 0)
                {
                    updatedEntries.Add(spreadsheet.Key, spreadsheet.Value);
                }
            }
            return updatedEntries;
        }
    }
}
